/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IG.java
 *
 * Created on Dec 1, 2009, 10:00:35 AM
 */
package Interface;

import java.io.File;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import moteur.CartesTransport;
import moteur.Transport;
import moteur.carte.Chemin;
import moteur.carte.EmplacementLegal;
import moteur.carte.editor.Graphe;
import moteur.carte.MouvementLegal;
import moteur.paquet.PaquetCartesVoyages;
import moteur.paquet.PaquetEnum;
import moteur.paquet.PaquetPionTransport;
import moteur.paquet.PaquetVideException;
import moteur.paquet.PionsEnMain;
import moteur.paquet.TypeMismatchException;
import moteur.partie.Joeur;
import moteur.partie.Jouer;
import moteur.partie.Partie;

/**
 *
 * @author nlupins
 */
public class IG extends javax.swing.JFrame implements ListenerPartie, ListenerJoeur {
    AffichageCarte ac;
    AffichagePions ap;
    DataPublic dp;
    Jouer joeurCourant = null;
    private Graphe g;
    Partie.Etat.Phase phase = null;

    /** Creates new form IG */
    public IG() {
        //Unzip.start("data.zip");
        initComponents();
        this.cartes.setModel(new javax.swing.DefaultListModel());
        this.choix.setModel(new javax.swing.DefaultListModel());
        this.pions.setModel(new javax.swing.DefaultListModel());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dataJoeurCourant = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        cartes = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        pions = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        choix = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        vuePlateau = new javax.swing.JScrollPane();
        layers = new javax.swing.JLayeredPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        dataJoeurCourant.setPreferredSize(new java.awt.Dimension(400, 200));
        dataJoeurCourant.setLayout(new java.awt.GridLayout(2, 0));

        jLabel1.setText("Cartes En Main");
        jLabel1.setPreferredSize(new java.awt.Dimension(97, 10));
        dataJoeurCourant.add(jLabel1);

        jLabel2.setText("Pions En Main");
        dataJoeurCourant.add(jLabel2);

        jLabel3.setText("Choix de Pions");
        dataJoeurCourant.add(jLabel3);

        jButton1.setText("PASSER");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        dataJoeurCourant.add(jButton1);

        jScrollPane1.setViewportView(cartes);

        dataJoeurCourant.add(jScrollPane1);

        jScrollPane2.setViewportView(pions);

        dataJoeurCourant.add(jScrollPane2);

        jScrollPane3.setViewportView(choix);

        dataJoeurCourant.add(jScrollPane3);

        jButton2.setText("VALIDER");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        dataJoeurCourant.add(jButton2);

        getContentPane().add(dataJoeurCourant, java.awt.BorderLayout.SOUTH);

        vuePlateau.setAutoscrolls(true);
        vuePlateau.setDoubleBuffered(true);
        vuePlateau.setMaximumSize(new java.awt.Dimension(1024, 768));
        vuePlateau.setMinimumSize(new java.awt.Dimension(320, 240));
        vuePlateau.setPreferredSize(new java.awt.Dimension(320, 240));

        layers.setAutoscrolls(true);
        layers.setDoubleBuffered(true);
        layers.setOpaque(true);
        layers.setPreferredSize(new java.awt.Dimension(1024, 1024));
        vuePlateau.setViewportView(layers);

        getContentPane().add(vuePlateau, java.awt.BorderLayout.CENTER);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane4.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane4, java.awt.BorderLayout.WEST);

        jMenu1.setText("Fichier");

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.META_MASK));
        jMenuItem4.setText("Nouveau");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.META_MASK));
        jMenuItem6.setText("Quitter");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Partie");

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem5.setText("Lancer");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem5);

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem7.setText("Terminer");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem7);

        jMenuBar1.add(jMenu3);

        jMenu2.setText("Editeur");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Nouveau graphe");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Charger un graphe");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Sauvegarder un graphe");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenu4.setText("Nouvel élément");

        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem8.setText("Nouvelle ville");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem8);

        jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem9.setText("Nouveau chemin");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem9);

        jMenuItem10.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem10.setText("Nouvelle rivière");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem10);

        jMenuItem11.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem11.setText("Nouveau lac");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem11);

        jMenu2.add(jMenu4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (joeurCourant != null) {
            joeurCourant.passe();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        Object[] options = {"Par défaut", "Choisir dans l'arborescence", "Annuler"};
        int reponse = JOptionPane.showOptionDialog(null, "Quel moyen choisissez-vous pour charger la carte ?", "New Game",
                JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, options[0]);
        if (reponse == 2) { // Bouton annuler
            return;
        } else if (reponse == 1) { // Choix entre toutes les cartes préenregistrées
            String temp = Graphe.mapChoice();
            g = Graphe.readGrapheAbs(temp);
        } else { // Choix de la carte par défaut
            g = Graphe.readGrapheAbs(getClass().getResource("graphe2.xml").getFile());
        } 
        // Lorsque le graphe est ENFIN défini on continu et on l'affiche
        Partie p = Partie.getInstance();
        p.setG(g);
        p.addListener(this);
        Joeur j1 = new JoeurGraphique("Alphonse", this);
        Joeur j2 = new JoeurGraphique("Bernard", this);
        Joeur j3 = new JoeurGraphique("Charles", this);
        Joeur j4 = new JoeurGraphique("Denis", this);
        j1.addListener(this);
        j2.addListener(this);
        j3.addListener(this);
        j4.addListener(this);
        dp = new DataPublic();
        getContentPane().add(dp, java.awt.BorderLayout.EAST);
        ac = new AffichageCarte(this);
        ap = new AffichagePions();
        //initComponents();
        layers.add(ac, 1);
        ac.setBounds(layers.getBounds());
        layers.add(ap, 0);
        ap.setBounds(layers.getBounds());
        p.start();
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        Object[] options = {"Oui", "Non"};
        int reponse = JOptionPane.showOptionDialog(null, "Voulez vous vraiment quitter ?", "Closing Game",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, options[0]);
        if (reponse == JOptionPane.YES_OPTION) {
            Partie.getInstance().finir();
            System.exit(0);
        }
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // Action pour creer un graphe
        g = new Graphe() ;
        Partie.getInstance().setG(g);
        ac = new AffichageCarte(this, true);
        ac.repaint();
        layers.add(ac, 1);
        ac.setBounds(layers.getBounds());
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.validation();
    }//GEN-LAST:event_jButton2ActionPerformed

    @SuppressWarnings("static-access")
    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        Partie p = Partie.getInstance();
        if (ac != null && ac.getEditing()) {
            p.getG().writeGraphe("current.xml");
            p.setG(null);
            ac.repaint() ;
            ac =null ;
            System.gc();
            ac = new AffichageCarte(this);
            layers.add(ac, 1);
            ac.setBounds(layers.getBounds());
            g = Graphe.readGraphe("current.xml");
            p.setG(g);
        }
        if (p.getG() == null) {
            JOptionPane.showMessageDialog(null, "Veuillez charger ou créer un graphe sur lequel le jeu se lancera.");
        }
        else {
            Joeur j1 = new JoeurGraphique("Alphonse", this);
            Joeur j2 = new JoeurGraphique("Bernard", this);
            Joeur j3 = new JoeurGraphique("Charles", this);
            Joeur j4 = new JoeurGraphique("Denis", this);
            p.addListener(this);
            j1.addListener(this);
            j2.addListener(this);
            j3.addListener(this);
            j4.addListener(this);
            layers.add(ap, 0);
            ap.setBounds(layers.getBounds());
            dp = new DataPublic();
            getContentPane().add(dp, java.awt.BorderLayout.EAST);
            p.start();
        }
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        if (Partie.getInstance().getG() == null)
            JOptionPane.showMessageDialog(null, "Aucune action : aucune partie n'était en cours.");
        else
            Partie.getInstance().finir();
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        Object[] options = { "Oui", "Non, choisir" };
        int reponse = JOptionPane.showOptionDialog(null, "Charger le graphe par défaut ?", "Loading Graohe",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, options[0]);
        if (reponse == JOptionPane.YES_OPTION) {
            try {
                g = Graphe.readGraphe();
            } catch (Exception ex) {
                Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else {
            String temp = Graphe.mapChoice();
            g = Graphe.readGrapheAbs(temp) ;
        }
        Partie.getInstance().setG(g);
        ac = new AffichageCarte(this);
        ap = new AffichagePions();
        layers.add(ac, 1);
        ac.setBounds(layers.getBounds());
        layers.add(ap, 0);
        ap.setBounds(layers.getBounds());
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        ac.setCurrentElement(0);
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        ac.setCurrentElement(1);
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        ac.setCurrentElement(2);
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        ac.setCurrentElement(3);
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    @SuppressWarnings("static-access")
    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        Partie.getInstance().getG().writeGraphe("test.xml");
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    @Override
    public void update(EventPartie e) {
        dp.repaint();
        joeurCourant = e.joeur;
        phase = e.phase;
        jTextArea1.append(" \n " + e.phase.name() + " \n ");
        //*
        if (e.phase != null) {

            if (e.phase == Partie.Etat.Phase.resolution) {
                System.out.println("\n \t -----FIN TOUR " + e.tourOUXscoreVainqueur + "-----");
            } else {
                System.out.println("\n" + "Phase : " + e.phase.name());
            }
        } else {


            if (e.joeur != null) {
                System.out.println(e.joeur.getNom() + " remporte la partie avec un score de: " + e.tourOUXscoreVainqueur);
            }
        }
        if (e.joeur != null) {
            System.out.println("A " + e.joeur.getNom() + " de jouer.");
        }
        //*/
    }

    /**
     *
     * @param e
     */
    @Override
    public void update(EventJoeur e) {
        //*
        if (e.position != null) {
            System.out.println(e.himself.getNom() + " s'est déplacé à " + e.position.toString() + " en passant par " + e.route.toString());
        }
        if (e.pion != null) {
            if (e.route != null) {
                System.out.println(e.himself.getNom() + " a placé " + e.pion.toString() + " sur " + e.route.toString());
            } else {
                System.out.println(e.himself.getNom() + " a choisi un pion " + e.pion.name());
            }

        }
        if (e.pion == null && e.position == null && e.route == null) {
            System.out.println(e.himself.getNom() + " a passé.");
        }
        //*/
    }

    public void doSomething() {
        dp.repaint();
        if (phase != null) {
            switch (phase) {
                case pioche:
                    setCartes();
                    defausseValide();
                    jTextArea1.append("Sélectionner les cartes \nà défauser.");
                    break;
                case choisirPion:
                    setCartes();
                    setPions();
                    jTextArea1.append("Sélectionner un pions.");
                    break;
                case deplacer:
                    setCartes();
                    jTextArea1.append("Choisir lune ville et \nles cartes du paiement.");
                    break;
                case placerPion:
                    setCartes();
                    setPions();
                    jTextArea1.append("Choisir un pion \net un chemin.");
                    break;
                default:
                    break;
            }
        }
    }

    public void validation() {
        if (phase != null) {
            switch (phase) {
                case pioche:
                    defausseValide();
                    break;
                case choisirPion:
                    choixValide();
                    break;
                case deplacer:
                    mouvementValide();
                    break;
                case placerPion:
                    placementValide();
                    break;
                default:
                    break;
            }
        }
    }

    private void setCartes() {
        Vector<CartesTransport> v = new Vector<CartesTransport>();
        byte[] t = ((Joeur) joeurCourant).getCartes().getPaquet();
        for (int i = t.length; --i >= 0;) {
            for (int j = t[i]; --j >= 0;) {
                v.addElement(CartesTransport.values()[i]);
            }
        }
        cartes.setListData(v);
    }

    private void setPions() {

        Vector<Transport> v = new Vector<Transport>();
        byte[] t = ((Joeur) joeurCourant).getPions().getAll();
        for (int i = t.length; --i >= 0;) {
            for (int j = t[i]; --j >= 0;) {
                v.addElement(Transport.values()[i]);
            }
        }
        pions.setListData(v);
    }

    public void setChoix(PionsEnMain ch) {
        Vector<Transport> v = new Vector<Transport>();
        if (ch != null) {

            byte[] t = ch.getPaquet();
            for (int i = t.length; --i >= 0;) {
                for (int j = t[i]; --j >= 0;) {
                    v.addElement(Transport.values()[i]);
                }
            }
        }
        choix.setListData(v);
    }

    private void defausseValide() {
        try {
            int tmp = ((Joeur) joeurCourant).getCartes().total();
            Object[] ob = cartes.getSelectedValues();
            if (tmp - ob.length < 5) {
                PaquetEnum cem = ((Joeur) joeurCourant).getCartes();
                PaquetEnum pcv = PaquetCartesVoyages.getInstance();
                for (Object o : ob) {
                    cem.transfer((CartesTransport) o, pcv);
                }
                joeurCourant.updatePartie();
                joeurCourant = null;
                repaint();
            }
        } catch (Exception ex) {
            Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void choixValide() {
        if (choix.getSelectedValues().length == 1) {
            try {
                Partie.getInstance().getTmpPions().transfer((Transport) choix.getSelectedValue(), ((Joeur) joeurCourant).getPions());
            } catch (TypeMismatchException ex) {
                Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
            } catch (PaquetVideException ex) {
                Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
            }
            joeurCourant.updatePartie();
            joeurCourant = null;
            repaint();

        }
        if (choix.getSelectedValues().length == 0) {
            try {
                ((Joeur) joeurCourant).getPions().piocherCaché(PaquetPionTransport.getInstance());
            } catch (PaquetVideException ex) {
                Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TypeMismatchException ex) {
                Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
            }
            joeurCourant.updatePartie();
            joeurCourant = null;
            repaint();
        }
    }

    private void mouvementValide() {
        Vector<MouvementLegal> ML = ap.getMl();
        Object[] ct = cartes.getSelectedValues();

        for (MouvementLegal ml : ML) {
            CartesTransport t = null;
            Transport tmp = ml.getT();
            if (ml.getT() == null) {
                if (ac.getSelected() == ml.getDest() && ml.getCost() == ct.length) {
                    PaquetEnum cem = ((Joeur) joeurCourant).getCartes();
                    PaquetEnum pcv = PaquetCartesVoyages.getInstance();

                    for (Object o : ct) {
                        t = (CartesTransport) o;
                        try {
                            cem.transfer(t, pcv);
                        } catch (TypeMismatchException ex) {
                            Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (PaquetVideException ex) {
                            Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    ((Joeur) joeurCourant).bouge(ml.getDest());
                    joeurCourant.updatePartie();
                    joeurCourant = null;
                    ap.setEl(null);
                    ap.setMl(null);
                    ap.repaint();
                    break;
                }
            } else {
                boolean b = true;
                for (Object o : ct) {
                    t = (CartesTransport) o;
                    b = b && (t.ordinal() == tmp.ordinal());
                }
                if (b) {
                    PaquetEnum cem = ((Joeur) joeurCourant).getCartes();
                    PaquetEnum pcv = PaquetCartesVoyages.getInstance();
                    try {
                        cem.transferNb(CartesTransport.values()[tmp.ordinal()], (int) ml.getCost(), pcv);
                    } catch (TypeMismatchException ex) {
                        Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (PaquetVideException ex) {
                        Logger.getLogger(IG.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    ((Joeur) joeurCourant).bouge(ml.getDest());
                    joeurCourant.updatePartie();
                    joeurCourant = null;
                    ap.setEl(null);
                    ap.setMl(null);
                    repaint();
                    break;
                }

            }
        }

    }

    private void placementValide() {
        if (ac.getSelected() instanceof Chemin) {
            Chemin cl, cs;
            for (EmplacementLegal el : ap.getEl()) {
                cl = el.getC();
                cs = (Chemin) ac.getSelected();
                if (cs == cl && ((Transport) pions.getSelectedValue()) == el.getT()) {
                    cl.setPion(el.getT());
                    ((Joeur) joeurCourant).getPions().retirerSiPossible(el.getT());
                    joeurCourant.updatePartie();
                    joeurCourant = null;
                    ap.setEl(null);
                    ap.setMl(null);
                    repaint();
                    break;
                }
            }
        }
    }

    public void test() {

        Joeur j1 = new JoeurGraphique("Alphonse", this);
        j1.addListener(this);
        Joeur j2 = new JoeurGraphique("Bernard", this);
        j2.addListener(this);
        Joeur j3 = new JoeurGraphique("Charles", this);
        j3.addListener(this);
        Joeur j4 = new JoeurGraphique("Denis", this);
        j4.addListener(this);

        Partie.getInstance().run();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                IG ig = new IG();
                ig.setVisible(true);
                //ig.test();

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JList cartes;
    private javax.swing.JList choix;
    private javax.swing.JPanel dataJoeurCourant;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JScrollPane jScrollPane2;
    javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLayeredPane layers;
    private javax.swing.JList pions;
    private javax.swing.JScrollPane vuePlateau;
    // End of variables declaration//GEN-END:variables
}
